In [1]:
import matplotlib.pyplot as plt
import matplotlib.patches as mp
import numpy as np
import qqmbr.odebook as ob
import warnings

Задача 1.

In [2]:
import plotly
import plotly.graph_objs as go
from plotly.offline import iplot as plot
from plotly.offline import init_notebook_mode

init_notebook_mode()

t = np.linspace(0, np.pi, 1000)

c1 = 1
c2 = -10

trace1 = go.Scatter3d(
    x = c2 * (np.sin(6 * t) + np.cos(6 * t)) - c1 * np.sin(6 * t),
    y = 2 * c2 * np.sin(6 * t) + c1 * (np.cos(6 * t) - np.sin(6 * t)),
    z = t,
    mode = 'lines+markers',
    marker = {'size': 3},
    hoverinfo = 'none',
    name = 'plot',
    line = dict(
        color = ('rgb(240, 128, 128)'),
        width = 2
    ),
    #projection = {'x': {'show': True, 'opacity': 1}},
)

trace3 = go.Scatter3d(
    x = np.zeros_like(t),
    y = np.zeros_like(t),
    z = t,
    mode = 'lines',
    hoverinfo = 'none',
    name = 'x=0, y=0',
    line = dict(
        color = ('rgb(1, 128, 128)')
    )
)
layout = go.Layout(
                width = 800, 
                height = 600, 
)
plot(dict(data=[trace1, trace3], layout=layout))
In [3]:
fontsize = 20
figure = plt.figure(figsize=(10, 10))
x_min = -7.5
x_max = 7.5
y_min = -7.5
y_max = 7.5
ob.axes4x4(xmin=x_min - 0.1, xmax=x_max, ymin=y_min - 0.1, ymax=y_max, fontsize=fontsize, labels=('x', 'y'))
ax = figure.gca()

#fs = lambda x, y: np.array([-6 * x + 12 * y, -6 * x + 6 * y]) / np.linalg.norm(np.array([-6 * x + 12 * y, -6 * x + 6 * y]))
#ob.mquiver(np.linspace(x_min, x_max, 20), np.linspace(y_min, y_max, 20),
#           fs, color='Teal', headlength=5, scale=3, scale_units='x') 

start_points = [1, 2, 3, 4]
inits  = [(x,  -1) for x in start_points]
inits += [(x,   1) for x in start_points]
inits += [(-x,  1) for x in start_points]
inits += [(-x, -1) for x in start_points]
ob.phaseportrait(lambda X: np.array([-6 * X[0] + 12 * X[1], -6 * X[0] + 6 * X[1]]), 
                 inits, t=(-2, 2), n=100, linewidth=3, color='LIGHTCORAL')

ax.tick_params(axis=u'both', which=u'both',length=0)
plt.legend()
plt.show()

Задача 2.

In [4]:
from itertools import product
from scipy.integrate import odeint
from scipy.optimize import bisect

C = np.array([[2, 1], [1, -1]])
A = C @ np.diag([1, -1]) @ np.linalg.inv(C)

def find_sep(f, init_func, test_func, T=10):
    def my_test(s):
        init = init_func(s)
        out = odeint(f, init, [0, T])[1]
        return test_func(out)
    return init_func(bisect(my_test, 0, 1))

def linear_homotopy(A, B, s):
    return A*(1-s) + B*s

def linear(X, t=0):
    return np.tensordot(A, X, axes=(1, 0))

def draw_seps(field):
    stable_sep_inits = [find_sep(field, 
                            lambda s: linear_homotopy(*endpoints, s),
                            lambda X: X[0] + X[1], T=20)
                        for endpoints in np.array([[[-3, 0], [0, 3]],
                                                  [[0, -3], [3, 0]]])]

    unstable_sep_inits = [find_sep(lambda X, t: -field(X, t), 
                            lambda s: linear_homotopy(*endpoints, s),
                            lambda X: X[0] - X[1], T=20)
                        for endpoints in np.array([[[-3, 0], [0, -3]],
                                                  [[0, 3], [3, 0]]])]
    ob.phaseportrait(field, stable_sep_inits, t=(-1, 5), linewidth=3, color='Teal')
    ob.phaseportrait(field, unstable_sep_inits, t=(-5, 1), linewidth=3, color='Teal')


plt.figure(figsize=(10, 10))
ob.axes4x4(labels=('x','y'), fontsize=14)

x = np.linspace(-4, 4)
y = np.linspace(-4, 4)
Z = np.meshgrid(x, y)
W = linear(Z)
plt.streamplot(*Z, *W, color='LIGHTCORAL', linewidth=3)

draw_seps(linear)

Задача 3.

In [5]:
def draw_axes(xmin, xmax, ymin, ymax, labels=("x", "y"), temp=0):
    plt.axis([xmin, xmax, ymin, ymax])
    ob.center_spines(None, xmin, temp)
    plt.text(xmax - 0.1, temp + 0.1, "$%s$" % labels[0],fontsize=20, verticalalignment='bottom', horizontalalignment='right')
    plt.text(xmin + 0.2, ymax - 0.1, "$%s$" % labels[1],fontsize=20, verticalalignment='top', horizontalalignment='right')

plt.figure(figsize=(10, 10))
plt.subplot2grid((10, 10), (0, 0), rowspan=8, colspan=10)
draw_axes(7, 11, -2, 2, labels=("x", "\dot x"))

x = np.linspace(7, 11)
epsilon = 3
plt.plot(x, (x - 9) * (-(x - 9) ** 2 - 4 + epsilon), '-', lw=4, color='LIGHTCORAL')

plt.subplot2grid((10, 10), (9, 0), colspan=10)
plt.yticks([])
plt.xlim(6.8, 11)

ob.onedim_phasecurves(7, 11, [9], [1, -1], orientation='horizontal')
In [6]:
plt.figure(figsize=(10, 10))
plt.subplot2grid((10, 10), (0, 0), rowspan=8, colspan=10)
draw_axes(7, 11, -2, 2, labels=("x", "\dot x"))

x = np.linspace(7, 11)
epsilon = 4
plt.plot(x, (x - 9) * (-(x - 9) ** 2 - 4 + epsilon), '-', lw=4, color='LIGHTCORAL')

plt.subplot2grid((10, 10), (9, 0), colspan=10)
plt.yticks([])
plt.xlim(6.8, 11)

ob.onedim_phasecurves(7, 11, [9], [1, -1], orientation='horizontal')
In [7]:
plt.figure(figsize=(10, 10))
plt.subplot2grid((10, 10), (0, 0), rowspan=8, colspan=10)
draw_axes(7, 11, -2, 2, labels=("x", "\dot x"))

x = np.linspace(7, 11)
epsilon = 5
plt.plot(x, (x - 9) * (-(x - 9) ** 2 - 4 + epsilon), '-', lw=4, color='LIGHTCORAL')

plt.subplot2grid((10, 10), (9, 0), colspan=10)
plt.yticks([])
plt.xlim(6.8, 11)

ob.onedim_phasecurves(7, 11, [8, 9, 10], [1, -1, 1, -1], orientation='horizontal')

Задача 4.

In [8]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fontsize = 20
x_min = -5
x_max = -3
y_min = 1
y_max = 3

Y, X = np.mgrid[y_min : y_max : 1000j, x_min : x_max : 1000j]
U = 2 * Y - 7 * np.log(X + 5) - 4
V = 2 * X - 14 * Y ** 2 + 49 * Y - 34

figure = plt.figure(figsize=(10, 10))
plt.subplot2grid((10, 10), (0, 0), rowspan=10, colspan=10)
draw_axes(x_min, x_max, y_min, y_max, labels=("x", "y"), temp=y_min)
ax = figure.gca()
ax.streamplot(X, Y, U, V, density=[1.5, 1.5], color='LIGHTCORAL', linewidth=3)

plt.tight_layout()
plt.show()
In [9]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fontsize = 20
x_min = -4 - 0.5
x_max = -4 + 0.5
y_min = 2 - 0.5
y_max = 2 + 0.5

Y, X = np.mgrid[y_min : y_max : 1000j, x_min : x_max : 1000j]
U = -7 * X + 2 * Y
V = 2 * X - 7 * Y

figure = plt.figure(figsize=(10, 10))
plt.subplot2grid((10, 10), (0, 0), rowspan=10, colspan=10)
draw_axes(x_min, x_max, y_min, y_max, labels=("x", "y"), temp=y_min)
ax = figure.gca()
ax.streamplot(X, Y, U, V, density=[1.5, 1.5], color='LIGHTCORAL', linewidth=3)

plt.tight_layout()
plt.show()